package cn.fay.chapter13;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TheReplacements {
/*! Here's a block of text to use as input to
    the regular expression matcher. Note that we'll
    first extract the block of text by looking for
    the special delimiters, then process the
    extracted block. !*/
	public static void main(String[] args) {
		
		File file=new File("src/cn/fay/chapter13/TheReplacements.java");
		try {
			BufferedReader bufin=new BufferedReader(new FileReader(file));
			String line=null;
			StringWriter sw=new StringWriter();
			BufferedWriter writer=new BufferedWriter(sw);
			while ((line=bufin.readLine())!=null) {
				writer.write(line);
				writer.newLine();
			}
			bufin.close();
			writer.close();
			String buffer = sw.getBuffer().toString();
			Matcher mInput = Pattern.compile("/\\*!(.*)!\\*/", Pattern.DOTALL).matcher(buffer);
			if (mInput.find()) 
				buffer=mInput.group(1);
			
			buffer=buffer.replaceAll(" {2,}", " ");
			buffer=buffer.replaceAll("(?m)^ +", "");
			System.out.println(buffer);
			buffer=buffer.replaceFirst("[aeiou]", "(VOWEL1)");
			//System.out.println(buffer);
			StringBuffer sbuf=new StringBuffer();
			Pattern p = Pattern.compile("[aeiou]");
			Matcher m = p.matcher(buffer);
			while (m.find()) 
				m.appendReplacement(sbuf, m.group().toUpperCase());
			m.appendTail(sbuf);
			System.out.println(sbuf);
				
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
